package com.luli.Utils;

import com.luli.entity.response.ResponseHead;
import com.luli.entity.response.ResponseMsg;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * 陆离
 * 2022/5/5 15:02:39
 * 封装返回请求提，项目需要严格按此返回
 */
public class ResponseMsgUtil {
    /**
     * 成功返回的请求体
     * @param requestId
     * @param info
     * @param data
     * @return
     */
    public static ResponseMsg<?> getSuccessMsg(String requestId, String info, Object data){
        ResponseHead responseHead = new ResponseHead();
        responseHead.setRespDesc(info);
        responseHead.setRespCode("00");
        responseHead.setRespStatus("00");
        responseHead.setResponseTime(getNowDateTime());
        responseHead.setRequestId(requestId==null? UUID.randomUUID().toString():requestId);
        ResponseMsg msg=new ResponseMsg();
        msg.setHead(responseHead);
        msg.setData(data);
        return msg;
    }

    /**
     * 失败返回的请求体
     * @param requestId
     * @param info
     * @param object
     * @return
     */
    public static  ResponseMsg<?> getFailMsg(String requestId,String info,Object object){
        ResponseMsg msg=new ResponseMsg();
        ResponseHead responseHead = new ResponseHead();
        responseHead.setRespDesc(info);
        responseHead.setRespCode("01");
        responseHead.setRespStatus("01");
        responseHead.setResponseTime(getNowDateTime());
        responseHead.setRequestId(requestId==null?UUID.randomUUID().toString():requestId);
        msg.setHead(responseHead);
        msg.setData(object);
        return msg;
    }

    private static String getNowDateTime(){
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(d);
    }
}
